{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> start QUANTAXIS\n",
      "QUANTAXIS>> Selecting the Best Server IP of TDX\n",
      "QUANTAXIS>> === The BEST SERVER ===\n",
      " stock_ip 115.238.90.165 future_ip 61.152.107.141\n",
      "QUANTAXIS>> Welcome to QUANTAXIS, the Version is remake-version\n",
      "QUANTAXIS>>  \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``########`````##````````##``````````##`````````####````````##```##########````````#``````##``````###```##`````######`` \n",
      "  `##``````## ```##````````##`````````####````````##`##```````##```````##```````````###``````##````##`````##```##`````##` \n",
      "  ##````````##```##````````##````````##`##````````##``##``````##```````##``````````####```````#```##``````##```##``````## \n",
      "  ##````````##```##````````##```````##```##```````##```##`````##```````##`````````##`##```````##`##```````##````##``````` \n",
      "  ##````````##```##````````##``````##`````##``````##````##````##```````##````````##``###```````###````````##`````##`````` \n",
      "  ##````````##```##````````##``````##``````##`````##`````##```##```````##```````##````##```````###````````##``````###```` \n",
      "  ##````````##```##````````##`````##````````##````##``````##``##```````##``````##``````##`````##`##```````##````````##``` \n",
      "  ##````````##```##````````##````#############````##```````##`##```````##`````###########`````##``##``````##`````````##`` \n",
      "  ###```````##```##````````##```##```````````##```##```````##`##```````##````##`````````##```##```##``````##```##`````##` \n",
      "  `##``````###````##``````###``##`````````````##``##````````####```````##```##``````````##``###````##`````##````##`````## \n",
      "  ``#########``````########```##``````````````###`##``````````##```````##``##````````````##`##``````##````##`````##````## \n",
      "  ````````#####`````````````````````````````````````````````````````````````````````````````````````````````````````####` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      "  ``````````````````````````Copyright``yutiansut``2017``````QUANTITATIVE FINANCIAL FRAMEWORK````````````````````````````` \n",
      "  ``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " ```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` \n",
      " \n"
     ]
    }
   ],
   "source": [
    "import QUANTAXIS as QA\n",
    "import threading\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "user = QA.QA_Portfolio()\n",
    "# 创建两个account\n",
    "#这里是创建一个资产组合,然后在组合里面创建两个account  你可以想象成股票里面的两个策略账户\n",
    "#然后返回的是这个账户的id\n",
    "a_1 = user.new_account()\n",
    "a_2 = user.new_account()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Acc_2rPgV3wU'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_MARKET with ['backtest'] Broker >"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception in thread backtest:\n",
      "Traceback (most recent call last):\n",
      "  File \"C:\\ProgramData\\Anaconda3\\lib\\threading.py\", line 916, in _bootstrap_inner\n",
      "    self.run()\n",
      "TypeError: run() missing 1 required positional argument: 'event'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "然后这里 是创建一个交易前置  你可以理解成 创建了一个无界面的通达信客户端\n",
    "然后start()开启这个客户端\n",
    "连接到backtest的broker上 这个broker可以更换\n",
    "\"\"\"\n",
    "# 创建一个交易前置\n",
    "market = QA.QA_Market()\n",
    "# 交易前置连接broker \n",
    "market.start()\n",
    "market.connect(QA.RUNNING_ENVIRONMENT.BACKETEST)\n",
    "\n",
    "# 打印market\n",
    "print(market)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Acc_fM3wPlT8', 'Acc_YSItAdwk']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\"\"\"\n",
    "登陆到这个交易前置上 把你刚才的两个账户\n",
    "\"\"\"\n",
    "# 登陆交易\n",
    "market.login(QA.BROKER_TYPE.BACKETEST,a_1)\n",
    "market.login(QA.BROKER_TYPE.BACKETEST,a_2)\n",
    "# 打印市场中的交易账户\n",
    "print(market.get_account_id())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#然后这里 往交易前置里面添加订单 这个操作是异步的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive_order\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_id=a_1, amount=100000,price=None, amount_model=QA.AMOUNT_MODEL.BY_PRICE,time='2017-12-01', code='600010', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   data_type=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order datetime:2017-12-01 09:31:00 code:600010 price:2.62 towards:1 btype:0x01 order_id:Order_C4kJOhQP account:Acc_fM3wPlT8 status:300 >"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_id=a_1, amount=100000,price=None, amount_model=QA.AMOUNT_MODEL.BY_PRICE,time='2017-12-01', code='000001', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   data_type=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive_order< QA_Order datetime:2017-12-01 09:31:00 code:600010 price:2.62 towards:1 btype:0x01 order_id:Order_C4kJOhQP account:Acc_fM3wPlT8 status:300 >\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n",
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_id=a_2, amount=100000,price=None, amount_model=QA.AMOUNT_MODEL.BY_PRICE,time='2017-12-14', code='000001', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.BUY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   data_type=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive_order\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n下单以后 现金不会减少 但是可用现金会被扣除\\n因为如果是市价单 你的成交价未定\\n没法直接减少现金\\n可用现金减少 cash不减少 等到settle 等到成功交易的时候 才会扣cash\\n\\n'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order datetime:2017-12-01 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_6NI4oupJ account:Acc_fM3wPlT8 status:300 >\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "\"\"\"\n",
    "下单以后 现金不会减少 但是可用现金会被扣除\n",
    "因为如果是市价单 你的成交价未定\n",
    "没法直接减少现金\n",
    "可用现金减少 cash不减少 等到settle 等到成功交易的时候 才会扣cash\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order datetime:2017-12-01 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_6NI4oupJ account:Acc_fM3wPlT8 status:300 >"
     ]
    },
    {
     "data": {
      "text/plain": [
       "954300"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do\n"
     ]
    }
   ],
   "source": [
    "market.session[a_1].cash_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order datetime:2017-12-14 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_3v01gDsn account:Acc_YSItAdwk status:300 >"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1000000]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "< QA_Order datetime:2017-12-14 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_3v01gDsn account:Acc_YSItAdwk status:300 >\n"
     ]
    }
   ],
   "source": [
    "market.session[a_1].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n这里是交易前置内部的订单队列\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "这里是交易前置内部的订单队列\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>amount</th>\n",
       "      <th>amount_model</th>\n",
       "      <th>code</th>\n",
       "      <th>data_type</th>\n",
       "      <th>date</th>\n",
       "      <th>datetime</th>\n",
       "      <th>market_type</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_model</th>\n",
       "      <th>price</th>\n",
       "      <th>sending_time</th>\n",
       "      <th>status</th>\n",
       "      <th>strategy</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>transact_time</th>\n",
       "      <th>type</th>\n",
       "      <th>user</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>order_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Order_C4kJOhQP</th>\n",
       "      <td>Acc_fM3wPlT8</td>\n",
       "      <td>38100</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>600010</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_C4kJOhQP</td>\n",
       "      <td>close</td>\n",
       "      <td>2.62</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Order_6NI4oupJ</th>\n",
       "      <td>Acc_fM3wPlT8</td>\n",
       "      <td>7600</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>000001</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_6NI4oupJ</td>\n",
       "      <td>close</td>\n",
       "      <td>13.00</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Order_3v01gDsn</th>\n",
       "      <td>Acc_YSItAdwk</td>\n",
       "      <td>7600</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>000001</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-14</td>\n",
       "      <td>2017-12-14 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_3v01gDsn</td>\n",
       "      <td>close</td>\n",
       "      <td>13.00</td>\n",
       "      <td>2017-12-14 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               account_cookie  amount amount_model    code data_type  \\\n",
       "order_id                                                               \n",
       "Order_C4kJOhQP   Acc_fM3wPlT8   38100    by_amount  600010       day   \n",
       "Order_6NI4oupJ   Acc_fM3wPlT8    7600    by_amount  000001       day   \n",
       "Order_3v01gDsn   Acc_YSItAdwk    7600    by_amount  000001       day   \n",
       "\n",
       "                      date             datetime market_type        order_id  \\\n",
       "order_id                                                                      \n",
       "Order_C4kJOhQP  2017-12-01  2017-12-01 09:31:00        0x01  Order_C4kJOhQP   \n",
       "Order_6NI4oupJ  2017-12-01  2017-12-01 09:31:00        0x01  Order_6NI4oupJ   \n",
       "Order_3v01gDsn  2017-12-14  2017-12-14 09:31:00        0x01  Order_3v01gDsn   \n",
       "\n",
       "               order_model  price         sending_time  status strategy  \\\n",
       "order_id                                                                  \n",
       "Order_C4kJOhQP       close   2.62  2017-12-01 09:31:00     300     None   \n",
       "Order_6NI4oupJ       close  13.00  2017-12-01 09:31:00     300     None   \n",
       "Order_3v01gDsn       close  13.00  2017-12-14 09:31:00     300     None   \n",
       "\n",
       "                towards trade_id transact_time  type  user  \n",
       "order_id                                                    \n",
       "Order_C4kJOhQP        1     None          None  0x01  None  \n",
       "Order_6NI4oupJ        1     None          None  0x01  None  \n",
       "Order_3v01gDsn        1     None          None  0x01  None  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue()\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[< QA_Order datetime:2017-12-01 09:31:00 code:600010 price:2.62 towards:1 btype:0x01 order_id:Order_C4kJOhQP account:Acc_fM3wPlT8 status:100 >,\n",
       " < QA_Order datetime:2017-12-01 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_6NI4oupJ account:Acc_fM3wPlT8 status:100 >,\n",
       " < QA_Order datetime:2017-12-14 09:31:00 code:000001 price:13.0 towards:1 btype:0x01 order_id:Order_3v01gDsn account:Acc_YSItAdwk status:100 >]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue.trade_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#pending 是指的待成交列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>amount</th>\n",
       "      <th>amount_model</th>\n",
       "      <th>code</th>\n",
       "      <th>data_type</th>\n",
       "      <th>date</th>\n",
       "      <th>datetime</th>\n",
       "      <th>market_type</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_model</th>\n",
       "      <th>price</th>\n",
       "      <th>sending_time</th>\n",
       "      <th>status</th>\n",
       "      <th>strategy</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>transact_time</th>\n",
       "      <th>type</th>\n",
       "      <th>user</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>order_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Order_C4kJOhQP</th>\n",
       "      <td>Acc_fM3wPlT8</td>\n",
       "      <td>38100</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>600010</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_C4kJOhQP</td>\n",
       "      <td>close</td>\n",
       "      <td>2.62</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Order_6NI4oupJ</th>\n",
       "      <td>Acc_fM3wPlT8</td>\n",
       "      <td>7600</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>000001</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_6NI4oupJ</td>\n",
       "      <td>close</td>\n",
       "      <td>13.00</td>\n",
       "      <td>2017-12-01 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Order_3v01gDsn</th>\n",
       "      <td>Acc_YSItAdwk</td>\n",
       "      <td>7600</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>000001</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-14</td>\n",
       "      <td>2017-12-14 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_3v01gDsn</td>\n",
       "      <td>close</td>\n",
       "      <td>13.00</td>\n",
       "      <td>2017-12-14 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               account_cookie  amount amount_model    code data_type  \\\n",
       "order_id                                                               \n",
       "Order_C4kJOhQP   Acc_fM3wPlT8   38100    by_amount  600010       day   \n",
       "Order_6NI4oupJ   Acc_fM3wPlT8    7600    by_amount  000001       day   \n",
       "Order_3v01gDsn   Acc_YSItAdwk    7600    by_amount  000001       day   \n",
       "\n",
       "                      date             datetime market_type        order_id  \\\n",
       "order_id                                                                      \n",
       "Order_C4kJOhQP  2017-12-01  2017-12-01 09:31:00        0x01  Order_C4kJOhQP   \n",
       "Order_6NI4oupJ  2017-12-01  2017-12-01 09:31:00        0x01  Order_6NI4oupJ   \n",
       "Order_3v01gDsn  2017-12-14  2017-12-14 09:31:00        0x01  Order_3v01gDsn   \n",
       "\n",
       "               order_model  price         sending_time  status strategy  \\\n",
       "order_id                                                                  \n",
       "Order_C4kJOhQP       close   2.62  2017-12-01 09:31:00     300     None   \n",
       "Order_6NI4oupJ       close  13.00  2017-12-01 09:31:00     300     None   \n",
       "Order_3v01gDsn       close  13.00  2017-12-14 09:31:00     300     None   \n",
       "\n",
       "                towards trade_id transact_time  type  user  \n",
       "order_id                                                    \n",
       "Order_C4kJOhQP        1     None          None  0x01  None  \n",
       "Order_6NI4oupJ        1     None          None  0x01  None  \n",
       "Order_3v01gDsn        1     None          None  0x01  None  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue.pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker_trade\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "这个_trade是一个私有方法 只有模拟盘和回测才会有 实盘就是真的交易了 \n",
    "这个_trade是backtest类去调用的\n",
    "\n",
    "\"\"\"\n",
    "market._trade(QA.QA_Event(broker_name=QA.BROKER_TYPE.BACKETEST,after_success=None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'下面这两个是 查询  一个是异步查询 一个是同步的(no_wait)\\n异步不会阻塞当前线程 同步会阻塞'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ON TRADE\n"
     ]
    }
   ],
   "source": [
    "\"\"\"下面这两个是 查询  一个是异步查询 一个是同步的(no_wait)\n",
    "异步不会阻塞当前线程 同步会阻塞\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== SETTLED None ====="
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "account_settle\n"
     ]
    }
   ],
   "source": [
    "market.query_data(broker_name=QA.BROKER_TYPE.BACKETEST,data_type=QA.FREQUENCE.DAY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                 code='600010',start='2017-12-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 3 tasks to do\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([['000001', '13.15', '13.31', '12.91', '13.0', '1001997.0',\n",
       "        '2017-12-14']],\n",
       "      dtype='<U10')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account_settle\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 2 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker_settle"
     ]
    }
   ],
   "source": [
    "market.query_data_no_wait(broker_name=QA.BROKER_TYPE.BACKETEST,data_type=QA.FREQUENCE.DAY,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                 code='000001',start='2017-12-14')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "< QA_Account Acc_fM3wPlT8>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "query_data\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 4 tasks to do"
     ]
    }
   ],
   "source": [
    "\"\"\"成交了以后 你可以看到账户的资产变化了\"\"\"\n",
    "market.session[a_1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'待成交列表被清空'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 3 tasks to do\n"
     ]
    }
   ],
   "source": [
    "\"\"\"待成交列表被清空\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 2 tasks to do"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>amount</th>\n",
       "      <th>amount_model</th>\n",
       "      <th>code</th>\n",
       "      <th>data_type</th>\n",
       "      <th>date</th>\n",
       "      <th>datetime</th>\n",
       "      <th>market_type</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_model</th>\n",
       "      <th>price</th>\n",
       "      <th>sending_time</th>\n",
       "      <th>status</th>\n",
       "      <th>strategy</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>transact_time</th>\n",
       "      <th>type</th>\n",
       "      <th>user</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>order_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [account_cookie, amount, amount_model, code, data_type, date, datetime, market_type, order_id, order_model, price, sending_time, status, strategy, towards, trade_id, transact_time, type, user]\n",
       "Index: []"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue.pending"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'待成交队列清空'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ON QUERY\n",
      "[['600010' '2.6' '2.63' '2.56' '2.62' '920022.0' '2017-12-01']]"
     ]
    }
   ],
   "source": [
    "\"\"\"待成交队列清空\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue.trade_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1000000, 900028.267, 801080.067]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "cash 现金减少\n",
    "\"\"\"\n",
    "market.session[a_1].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001     7600.0\n",
       "600010    38100.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "因为没有触发每日结算时间 在T+1的市场 即使买入了也没有可卖的\n",
    "\"\"\"\n",
    "market.session[a_1].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sa=market.session[a_1].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ac=market.session[a_1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001     7600.0\n",
       "600010    38100.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ac.hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001     7600.0\n",
       "600010    38100.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2017-12-01 09:31:00',\n",
       "  '600010',\n",
       "  2.62,\n",
       "  38100.0,\n",
       "  'Order_C4kJOhQP',\n",
       "  'Trade_j6ONitmE',\n",
       "  149.733],\n",
       " ['2017-12-01 09:31:00',\n",
       "  '000001',\n",
       "  13.0,\n",
       "  7600.0,\n",
       "  'Order_6NI4oupJ',\n",
       "  'Trade_9GFr0cAJ',\n",
       "  148.2]]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1].history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001     7600.0\n",
       "600010    38100.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "持仓表增加\n",
    "\"\"\"\n",
    "market.session[a_1].hold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'body': {'account': {'cash': [1000000, 900028.267, 801080.067],\n",
       "   'history': [['2017-12-01 09:31:00',\n",
       "     '600010',\n",
       "     2.62,\n",
       "     38100.0,\n",
       "     'Order_C4kJOhQP',\n",
       "     'Trade_j6ONitmE',\n",
       "     149.733],\n",
       "    ['2017-12-01 09:31:00',\n",
       "     '000001',\n",
       "     13.0,\n",
       "     7600.0,\n",
       "     'Order_6NI4oupJ',\n",
       "     'Trade_9GFr0cAJ',\n",
       "     148.2]]}},\n",
       " 'header': {'cookie': 'Acc_fM3wPlT8',\n",
       "  'current_time': None,\n",
       "  'portfolio': None,\n",
       "  'source': 'account',\n",
       "  'strategy_name': None,\n",
       "  'user': None}}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "账户信息\n",
    "\n",
    "可以看到 减少的资产 主要是因为收了手续费\n",
    "\"\"\"\n",
    "market.session[a_1].message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account_settle===== SETTLED None =====\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 2 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account_settle\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do"
     ]
    }
   ],
   "source": [
    "\"\"\"结算事件\"\"\"\n",
    "market._settle(QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker_settle"
     ]
    },
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001     7600.0\n",
       "600010    38100.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 3 tasks to do"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "结算完以后 可卖数量就会变成和持仓数一样\n",
    "\"\"\"\n",
    "market.session[a_1].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n结算完以后 待成交队列也被清空\\n'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 2 tasks to do\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "结算完以后 待成交队列也被清空\n",
    "\"\"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "receive_order\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_id=a_1, amount=market.session[a_1].sell_available.get('600010',0),price=None, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT,time='2017-12-05', code='600010', \n",
    "                    order_model=QA.ORDER_MODEL.CLOSE, towards=QA.ORDER_DIRECTION.SELL,market_type=QA.MARKET_TYPE.STOCK_CN,\n",
    "                   data_type=QA.FREQUENCE.DAY,broker_name=QA.BROKER_TYPE.BACKETEST)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "< QA_Order datetime:2017-12-05 09:31:00 code:600010 price:2.55 towards:-1 btype:0x01 order_id:Order_IJdHVW7X account:Acc_fM3wPlT8 status:300 >\n",
      "< QA_Order datetime:2017-12-05 09:31:00 code:600010 price:2.55 towards:-1 btype:0x01 order_id:Order_IJdHVW7X account:Acc_fM3wPlT8 status:300 >"
     ]
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue.trade_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>account_cookie</th>\n",
       "      <th>amount</th>\n",
       "      <th>amount_model</th>\n",
       "      <th>code</th>\n",
       "      <th>data_type</th>\n",
       "      <th>date</th>\n",
       "      <th>datetime</th>\n",
       "      <th>market_type</th>\n",
       "      <th>order_id</th>\n",
       "      <th>order_model</th>\n",
       "      <th>price</th>\n",
       "      <th>sending_time</th>\n",
       "      <th>status</th>\n",
       "      <th>strategy</th>\n",
       "      <th>towards</th>\n",
       "      <th>trade_id</th>\n",
       "      <th>transact_time</th>\n",
       "      <th>type</th>\n",
       "      <th>user</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>order_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Order_IJdHVW7X</th>\n",
       "      <td>Acc_fM3wPlT8</td>\n",
       "      <td>38100</td>\n",
       "      <td>by_amount</td>\n",
       "      <td>600010</td>\n",
       "      <td>day</td>\n",
       "      <td>2017-12-05</td>\n",
       "      <td>2017-12-05 09:31:00</td>\n",
       "      <td>0x01</td>\n",
       "      <td>Order_IJdHVW7X</td>\n",
       "      <td>close</td>\n",
       "      <td>2.55</td>\n",
       "      <td>2017-12-05 09:31:00</td>\n",
       "      <td>300</td>\n",
       "      <td>None</td>\n",
       "      <td>-1</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>0x01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               account_cookie  amount amount_model    code data_type  \\\n",
       "order_id                                                               \n",
       "Order_IJdHVW7X   Acc_fM3wPlT8   38100    by_amount  600010       day   \n",
       "\n",
       "                      date             datetime market_type        order_id  \\\n",
       "order_id                                                                      \n",
       "Order_IJdHVW7X  2017-12-05  2017-12-05 09:31:00        0x01  Order_IJdHVW7X   \n",
       "\n",
       "               order_model  price         sending_time  status strategy  \\\n",
       "order_id                                                                  \n",
       "Order_IJdHVW7X       close   2.55  2017-12-05 09:31:00     300     None   \n",
       "\n",
       "                towards trade_id transact_time  type  user  \n",
       "order_id                                                    \n",
       "Order_IJdHVW7X       -1     None          None  0x01  None  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.broker[QA.BROKER_TYPE.BACKETEST].order_handler.order_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "code\n",
       "000001    7600.0\n",
       "600010       0.0\n",
       "Name: amount, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "market.session[a_1].sell_available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker_trade"
     ]
    }
   ],
   "source": [
    "market._trade(QA.QA_Event(broker_name=QA.BROKER_TYPE.BACKETEST,after_success=None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1000000, 900028.267, 801080.067]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "market.session[a_1].cash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ON TRADE\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[['2017-12-01 09:31:00',\n",
       "  '600010',\n",
       "  2.62,\n",
       "  38100.0,\n",
       "  'Order_C4kJOhQP',\n",
       "  'Trade_j6ONitmE',\n",
       "  149.733],\n",
       " ['2017-12-01 09:31:00',\n",
       "  '000001',\n",
       "  13.0,\n",
       "  7600.0,\n",
       "  'Order_6NI4oupJ',\n",
       "  'Trade_9GFr0cAJ',\n",
       "  148.2],\n",
       " ['2017-12-05 09:31:00',\n",
       "  '600010',\n",
       "  2.55,\n",
       "  -38100.0,\n",
       "  'Order_IJdHVW7X',\n",
       "  'Trade_a8FIKTV3',\n",
       "  145.7325]]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 3 tasks to do"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "===== SETTLED None ====="
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "account_settle\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 2 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "account_settle\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine  <QA_ENGINE with ['backtest'] kernals>: There are still 1 tasks to do\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "broker_settle\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 3 tasks to do\n",
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 2 tasks to do\n",
      "QUANTAXIS>> From Engine < QA_Thread backtest >: There are still 1 tasks to do\n"
     ]
    }
   ],
   "source": [
    "market.session[a_1].history"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
